package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.minio.FileProperties;
import com.atguigu.lease.web.admin.service.FileService;
import io.minio.*;
import io.minio.errors.MinioException;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.DateFormat;
import java.util.Date;
import java.util.UUID;

@Service
@EnableConfigurationProperties(FileProperties.class)
public class FileServiceImpl implements FileService {
    @Autowired
    private FileProperties fileProperties;

    @Override
    public String upLoadFileService(MultipartFile file) {
        //上传图片的代码

        try {
            // 创建客户端对象
            MinioClient minioClient =
                    MinioClient.builder()
                            //端点：服务器的地址
                            .endpoint(fileProperties.getEndpoint())
                            //设置访问的用户名和密码
                            .credentials(fileProperties.getAccessKey(), fileProperties.getSecretKey())
                            .build();

            // 判断指定的桶是否存在
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(fileProperties.getBucketName()).build());
            if (!found) {
                // 如果桶不存在，则创建新桶
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(fileProperties.getBucketName()).build());
            } else {
                System.out.println("Bucket "+ fileProperties.getBucketName()+"already exists.");
            }

            // 上传图片
            // 'asiatrip'.
            UUID uuid = UUID.randomUUID();
            uuid.toString().replace("-","");

            //获取当前日期，进行指定格式的格式化
            String format = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
            String newFileName = format+"/"+uuid+"_"+file.getOriginalFilename();

            //获得图片的字节输入流
            InputStream inputStream = file.getInputStream();
            PutObjectArgs build = PutObjectArgs.builder()
                    //上传的对象是哪一个桶
                    .bucket(fileProperties.getBucketName())
                    //上传的对象在Minio存储的名字
                    .object(newFileName)
                    //上传的对象的流,第二个属性用于获取当前这个流中有多少可用的字节
                    .stream(inputStream, inputStream.available(), -1)
                    .build();

            //上传图片
            minioClient.putObject(build);

            //http://192.168.6.101:9000/lease/a.jpg

            String url = fileProperties.getEndpoint()+"/"+fileProperties.getBucketName()+"/"+newFileName;
            return url;
//            System.out.println(
//                    "'E:\\imgs\\ikun.jpg' is successfully uploaded as "
//                            + "object 'ikunchicken.jpg' to bucket 'test'.");
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
//                    System.out.println("HTTP trace: " + e.httpTrace());
        }
        return null;
    }
}


